import os
import logging
from logging import debug, info, error, warning
import numpy as np
import multiprocessing
import coloredlogs, logging
import dip_celery as dip
import dip as ndip
import time
from add_img import add_image

coloredlogs.install(level='DEBUG', logger=logging.getLogger(),
                    fmt='%(filename)s:%(lineno)d %(message)s',
                    datefmt='%H:%M:%S',
                    )
import sys
home = os.getenv('HOME', '')
database_root = sys.argv[1]
# database_root = home + '/data/baidupan/20170228'
# database_root = 'static/uploads'
# files = os.listdir(home + '/data/baidupan/20170228')
files = os.listdir(sys.argv[1])
num_thread = multiprocessing.cpu_count()
runningTasks = []

ind = 0
for f in files:
    ind += 1
    # if not f.endswith('.jpg'):
    #     continue
    f = database_root + '/' + f
    debug('adding ' + f + "  " + str(ind) + '/' + str(len(files)))
    # add_image(f)
    # try:
    # add_image(f)
    # except:
    #     logging.exception("something bad")

    while len(runningTasks) >= num_thread:
        removed = False
        for index in range(len(runningTasks)):
            if runningTasks[index].ready():
                finished_task = runningTasks.pop(index)
                finished_task.wait()
                removed = True
                break
        if not removed:
            time.sleep(0.1)
    runningTasks.append(add_image.delay(f))

    # add_image(f)
for task in runningTasks:
    task.wait()

